<?php

namespace Admin\Controller;

use Think\Model;

/**
 * 商品分类控制器
 * @author caomenglong
 */
class SpPcategoryController extends AdminController {

            
    /**
     * 展示商品分类
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function index($type='') {
        $name = I('name');
        $map['type'] =$type;
        $map['name'] = array('like', '%' . (string) $name . '%');
        $list = $this->lists('sp_pcategory', $map);
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '标题';
        $this->display();
    }

            
    
    /**
     * 添加商品分类
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function add($name = '', $sort = '', $type = '', $isDisposal = '') {
        if (IS_POST) {
             if($isDisposal=='false'){
                $isDisposal=0;
            }else{
                $isDisposal=1;
            }
            $gr_sp_pcategory = array(
                'name' => $name,
                'sort' => $sort,
                'type' => $type,
                'isDel' => 0,
                'status' => 1,
                'isDisposal' => $isDisposal,
                'createby' => get_nickname(UID),
                'createat' => NOW_TIME,
            );
            $id = M('sp_pcategory')->add($gr_sp_pcategory);
            if (!$id) {
                $this->error('添加失败！');
            } else {
                $this->success('添加成功！', U('index'));
            };
        } else {
            $this->display();
        }
    }
    
    /**
     * 修改商品分类
     * @author 曹梦龙 <138888611@qq.com>
     */ 
    public function edit($id = "", $name = '', $sort = '', $type = '', $isDisposal = '') {
        if (IS_POST) {            
            if($isDisposal=='false'){
                $isDisposal=0;
            }else{
                $isDisposal=1;
            }
            $gr_sp_pcategory = array(
                'name' => $name,
                'sort' => $sort,
                'type' => $type,
                'isDisposal' => $isDisposal,
            );
            
            if (!M('sp_pcategory')->where(array('id' => $id))->save($gr_sp_pcategory)) {
                $this->error('修改失败！');
            } else {
                $this->success('修改成功！', U('SpPcategory/index'));
            };
        } else {
            $data = M('sp_pcategory')->where(array('id' => $id))->find();
            $this->assign('data', $data);
            $this->display();
        }
    }
    
     
    /**
     * 删除方法
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function del($id = ""){
        if(!is_null($id)){            
            if (!M('sp_pcategory')->where(array('id' => $id))->delete()){
                $this->error('删除失败！');
            } else {
                $this->success('删除成功！', U('SpPcategory/index'));
            };
        }
    }
    
    /**
     * 商品分类状态修改
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function changeStatus($method = null, $id) {
        $id = is_array($id) ? implode(',', $id) : $id;
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] = array('in', $id);
        switch (strtolower($method)) {
            case 'forbid':
                $this->forbid('sp_pcategory', $map);
                break;
            case 'resume':
                $this->resume('sp_pcategory', $map);
                break;
            case 'delete':
                $this->delete('sp_pcategory', $map);
                break;
            default:
                $this->error('参数非法');
        }
    }

}
